<template>
  <div class="login-container">
    <div class="login-main">
      <h1>账号登录</h1>
      <el-form ref="mLoginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="账号"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            v-on:keyup.enter="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" v-on:click.prevent="handleLogin">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { useRouter, useRoute, LocationQueryValue } from 'vue-router'
import { useStore } from 'vuex'
import { ref, nextTick, computed, watch, defineComponent } from 'vue'
import { StateType as AppStateType } from '@/store/modules/app'
import { StateType as UserStateType } from '@/store/modules/system/user'

export default defineComponent({
  name: 'Login',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const store = useStore<{app: AppStateType; user: UserStateType}>()
    const password = ref<HTMLElement>()
    const mLoginForm = ref<HTMLElement>()
    const loginForm = ref({})
    const loginRules = ref({
      username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
      password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
    })
    const passwordType = ref('password')
    let otherQuery = {}
    let redirect: any
    const getOtherQuery = (query: Record<string, string | LocationQueryValue[] | null>) => {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          (acc as any)[cur] = query[cur]
        }
        return acc
      }, {})
    }
    const routePath = computed<string>(() => {
      return route.path
    })
    watch(routePath, () => {
      const query = route.query
      if (query) {
        redirect = query.redirect
        otherQuery = getOtherQuery(query)
      }
    }, { immediate: true })
    const loading = computed(() => {
      return store.state.app.isLoading
    })
    const handleLogin = () => {
      mLoginForm.value && (mLoginForm.value as any).validate((valid: boolean) => {
        if (valid) {
          store.dispatch('user/login', loginForm.value).then(() => {
            router.push({ path: redirect || '/', query: otherQuery })
          })
        }
      })
    }
    const showPwd = () => {
      if (passwordType.value === 'password') {
        passwordType.value = ''
      } else {
        passwordType.value = 'password'
      }
      nextTick(() => {
        password.value && password.value.focus()
      })
    }
    return {
      loading,
      password,
      showPwd,
      mLoginForm,
      handleLogin,
      passwordType,
      loginForm,
      loginRules
    }
  }
})
</script>

<style lang="scss" scoped>
.login-container{
  width: 100%;
  height: 100%;
  background: orange;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  .login-main{
    display: inline-block;
    border: 1px solid #e9eaeb;
    background: white;
    padding: 30px;
    max-width: 360px;
    width: 100%;
    margin: 0 5%;
    h1{
      color: #898989;
      font-size: 20px;
      font-weight: bold;
      text-align: left;
      margin-bottom: 20px;
    }
    .el-form-item{
      position: relative;
      .svg-container{
        position: absolute;
        z-index: 9;
        left: 10px;
      }
      .show-pwd{
        position: absolute;
        right: 10px;
      }
      .el-input{
        :deep input{
           padding: 12px 30px 12px 30px;
        }
      }
    }
  }
}
</style>
